package com.example.shangjinlieren.slice;
//import com.example.mask.Class.CustomLoading;
import com.example.mask.Class.CustomLoading;
import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.tabbar.entity.BottomNavItem;
import com.example.shangjinlieren.tabbar.fraction.FractionUtil;
import com.example.shangjinlieren.tabbar.model.BottomNavViewModel;
import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

public class MainAbilitySlice extends AbilitySlice {

//    底部导航所需变量 start
    private Integer currentSaveIndex=0;//当前正在显示的索引值
    private List<BottomNavItem> bottomNavItems;
    private Context context;
    private FractionUtil fractionUtil;
    List<Fraction> fractions;
    BottomNavViewModel bottomNavViewModel;
//    底部导航所需变量 end
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        present(new MainMoneyListDetailSlice(),intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        if(intent != null){
            String index = intent.getStringParam("index");

            if(index!=null && index!=""){
               this.currentSaveIndex =Integer.valueOf(index);
            }
        }
        Window window = getWindow();
        window.setBackgroundColor(MyColors.pageColorBg);
        //    底部导航所需变量 start
        this.bottomNavViewModel = new BottomNavViewModel();
        this.fractions =  bottomNavViewModel.initBottomNavFractionsBody(this);
        this.bottomNavItems=bottomNavViewModel.initBottomNavFractions();
        fractionUtil=new FractionUtil(fractions,currentSaveIndex,((FractionAbility)getAbility()),ResourceTable.Id_main_content);
        initBottom();

        //    底部导航所需变量 end
//        CustomLoading customLoading = null;
//        customLoading = new CustomLoading(getContext());
//        customLoading.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }
//    底部导航所需变量 start
    public void  initBottom(){
        //创建底部容器数据数组
        List<DirectionalLayout> tabList = new ArrayList<>();
        //获取主页面底部容器ID

        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        //顶部导航 nav_top_bar_title
//        DirectionalLayout NavTopBaCompontentr =(DirectionalLayout) findComponentById(ResourceTable.Id_nav_top_bar);

//        Text navTopBarTitle = (Text) parse.findComponentById(ResourceTable.Id_nav_top_bar_title);
//        navTopBarTitle.setText(this.bottomNavItems.get(this.currentSaveIndex).getTitle());
//        NavTopBaCompontentr.addComponent(parse);
        for (int i = 0; i <this.bottomNavItems.size() ; i++) {
            //获取模板容器
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())

                    .parse(ResourceTable.Layout_bottom_nav_tab, bottomTab, false);
            tabList.add(tab);
            //获取模板容器组件
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            buttonText.setTextSize((int) RpxUtil.rpx(20,getContext()));
            buttonImage.setHeight((int) RpxUtil.rpx(30,getContext()));
            buttonImage.setWidth((int) RpxUtil.rpx(30,getContext()));
            buttonImage.setScaleMode(Image.ScaleMode.INSIDE);

            if (buttonImage != null) {//
                if (i == this.currentSaveIndex) {
                    buttonImage.setPixelMap(this.bottomNavItems.get(i).getSelectImg());
                } else {
                    buttonImage.setPixelMap(this.bottomNavItems.get(i).getImg());
                }
            }
            if (buttonText != null) {
                buttonText.setText(this.bottomNavItems.get(i).getTitle());
                if (i == this.currentSaveIndex) {
                    int rgb = Color.getIntColor(this.bottomNavItems.get(i).getSelectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                } else {
                    int rgb = Color.getIntColor(this.bottomNavItems.get(i).getUnselectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                }

            }
            //绑定点击事件
            bottomNavBarClick(tab,i,tabList);
            bottomTab.addComponent(tab);
        }
        bottomNavBarClick(tabList.get(currentSaveIndex),currentSaveIndex,tabList);
    }
    public void bottomNavBarClick(DirectionalLayout item, int index,List<DirectionalLayout> tabList){
        //获取模板容器组件
//        DirectionalLayout Id_nav_top_bar =(DirectionalLayout) findComponentById(ResourceTable.Id_nav_top_bar);

        item.setClickedListener(component -> {
//            Text navTopBarTitle = (Text) findComponentById(ResourceTable.Id_nav_top_bar_title);

            for (int i = 0; i <this.bottomNavItems.size() ; i++) {
                Image buttonImage = (Image) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_img);
                Text buttonText = (Text) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_text);
                if (buttonImage != null&&buttonText != null) {//
                    if (i == index) {
                        System.out.println("当前索引"+index);
//                        navTopBarTitle.setText(this.bottomNavItems.get(index).getTitle());
                        buttonImage.setPixelMap(this.bottomNavItems.get(i).getSelectImg());
                        int rgb = Color.getIntColor(this.bottomNavItems.get(i).getSelectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    } else {
                        buttonImage.setPixelMap(this.bottomNavItems.get(i).getImg());
                        int rgb = Color.getIntColor(this.bottomNavItems.get(i).getUnselectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    }
                }
            }

//            if(index==1){
//                Id_nav_top_bar.setHeight(0);
//            }else {
//                Id_nav_top_bar.setHeight((int) RpxUtil.vp2px(40,getContext()));
//            }
            fractionUtil.show(index);
        });
//        if(index==1){
//            Id_nav_top_bar.setHeight(0);
//        }else {
//            Id_nav_top_bar.setHeight((int) RpxUtil.vp2px(40,getContext()));
//        }
        System.out.println("第一次");
        fractionUtil.show(index);
    }
// 底部导航所需变量 end
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
